// a function to place an element next to another
// everything here uses px
Element.implement({
	placeNextTo: function(el, options){
		
		$extend({
			attach: 'right', // top, left, bottom, right
	        align: 'center', // center, left-top, right-bottom
			offsets: {
	            x: 0,
	            y: 0
	        },
			distance: 10
		}, options);
        
		// xPos and yPos are not exactly what you think they are
		// thats the most compact way i found to position the msg box, its not meant to be readable
        var elCoord = el.getCoordinates(), xPos = 0, yPos = 0, o;
        
        switch(options.attach){
        case 'left':
        case 'right':
            o = {
                'style' : 'top',
                'opStyle' : 'bottom',
                'dim' : 'height',
                'axis' : 'y',
                'opAxis' : 'x'
            };
            break;
        case 'top':
        case 'bottom':
            o = {
                'style' : 'left',
                'opStyle' : 'right',
                'dim' : 'width',
                'axis' : 'x',
                'opAxis' : 'y'
            };
        }
        
        switch(options.attach){
        case 'left': case 'top':
            xPos = elCoord[options.attach] - this.getSize()[o.opAxis] - options.distance;
            break;
        case 'right': case 'bottom':
            xPos = elCoord[options.attach] + options.distance;
        }
        xPos = (xPos<0)?0:xPos;
        
        switch(options.align){
        case 'middle': case 'center':
            var elMiddle = elCoord[o.style] + elCoord[o.dim] / 2,
            	msgMiddle = this.getSize()[o.axis] / 2;
            yPos = elMiddle - msgMiddle;
            break;
        case 'left': case 'top': case 'left-top':
            yPos = elCoord[o.style];
            break;
        case 'right': case 'bottom': case 'right-bottom':
            yPos = elCoord[o.opStyle] - this.getSize()[o.axis];
        }
        
        switch(options.attach){
        case 'top': case 'bottom':
            var temp = xPos;
            xPos = yPos;
            yPos = temp;
        }
        
        yPos += options.offsets.y;
        xPos += options.offsets.x;
        
        this.setStyle('top',yPos);
        this.setStyle('left',xPos);
        
    }
});
